package com.xhwl.logistics.strategy.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xhwl.common.exception.BusinessException;
import com.xhwl.logistics.bo.VehicleCarRecordInParkingLotBO;
import com.xhwl.logistics.bo.VehicleCarportBO;
import com.xhwl.logistics.bo.cent.VehicleIntegratedConfigBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarRecordInParkingLotBO;
import com.xhwl.logistics.bo.input.QueryVehicleCarportBO;
import com.xhwl.logistics.bo.manipulate.VehicleSyncBO;
import com.xhwl.logistics.constant.VehicleConstant;
import com.xhwl.logistics.converter.mapstruct.CentBiConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleCarConverter;
import com.xhwl.logistics.converter.mapstruct.VehicleCarportConverter;
import com.xhwl.logistics.dao.mapper.IVehicleParkingLotMapper;
import com.xhwl.logistics.dto.http.CarInParkingLotCheAnDTO;
import com.xhwl.logistics.dto.http.CarportCheAnDTO;
import com.xhwl.logistics.dto.http.query.QueryCarInParkingLotCheAnDTO;
import com.xhwl.logistics.dto.http.query.QueryCommonCheAnDTO;
import com.xhwl.logistics.entity.VehicleParkingLot;
import com.xhwl.logistics.service.IVehicleParkingLotService;
import com.xhwl.logistics.service.cent.IBIVehicleService;
import com.xhwl.logistics.service.impl.http.CheAnHttpServiceImpl;
import com.xhwl.logistics.strategy.IVehicleParkingLotHandler;
import com.xhwl.logistics.util.AboutDateUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Comparator;
import java.util.Date;
import java.util.List;

@Slf4j
//@Service
public class VehicleParkingLotCheAnHandlerImpl implements IVehicleParkingLotHandler {

    @Autowired
    private CheAnHttpServiceImpl cheAnHttpService;

    @Autowired
    private IVehicleParkingLotService vehicleParkingLotService;

    @Autowired
    private IVehicleParkingLotMapper vehicleParkingLotMapper;

    @Autowired
    private IBIVehicleService biVehicleService;

    @Autowired
    private VehicleCarportConverter vehicleCarportConverter;

    @Autowired
    private VehicleCarConverter vehicleCarConverter;

    @Autowired
    private CentBiConverter centBiConverter;

    @Override
    public void sync(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, VehicleSyncBO vehicleSyncBO) {
        log.info("Che An Sync!");
        log.info("vehicleIntegratedConfigBO: " + vehicleIntegratedConfigBO);
    }

    @Override
    public VehicleCarportBO queryCarport(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryVehicleCarportBO queryVehicleCarportBO) {

        CarportCheAnDTO carportCheAnDTO = cheAnHttpService.queryCarport(vehicleIntegratedConfigBO.getVehicleParkingInfoBO(), QueryCommonCheAnDTO.getInstance(null));

        return vehicleCarportConverter.carportCheAnDTO2VehicleCarportBO(carportCheAnDTO);
    }

    @Override
    public VehicleCarRecordInParkingLotBO queryCarRecordInParkingLot(VehicleIntegratedConfigBO vehicleIntegratedConfigBO, QueryVehicleCarRecordInParkingLotBO queryVehicleCarRecordInParkingLotBO) {

        Integer projectId = queryVehicleCarRecordInParkingLotBO.getProjectId();
        String parkingLotName = queryVehicleCarRecordInParkingLotBO.getParkingLotName();
        if (!vehicleParkingLotService.judgeExistParkingLot(projectId, parkingLotName)) {
            throw new BusinessException("通过项目id和车场名称找不到车场");
        }

        List<CarInParkingLotCheAnDTO> carInParkingLotCheAnDTOList = cheAnHttpService.queryCarInParkingLot(vehicleIntegratedConfigBO.getVehicleParkingInfoBO(), QueryCommonCheAnDTO.getInstance(new QueryCarInParkingLotCheAnDTO(AboutDateUtil.dateToString(new Date()))));

        carInParkingLotCheAnDTOList.sort(Comparator.comparing(CarInParkingLotCheAnDTO::getEntertime).reversed());
        return vehicleCarConverter.toVehicleCarRecordInParkingLotBO(carInParkingLotCheAnDTOList, parkingLotName);
    }

    @Override
    public void statisticsToBi(VehicleIntegratedConfigBO vehicleIntegratedConfigBO) {
        log.info("Che An statistics to bi start!");

        try {
            Integer projectId = vehicleIntegratedConfigBO.getVehicleSystemInfoBO().getProjectId();

            VehicleParkingLot vehicleParkingLot = vehicleParkingLotMapper.selectOne(new LambdaQueryWrapper<VehicleParkingLot>().eq(VehicleParkingLot::getProjectId, projectId));
            String parkingLotName = vehicleParkingLot.getParkingLotName();

            Date processDate = new Date();

            Date ingestionDate = AboutDateUtil.theSameDayHourOfDate(processDate, VehicleConstant.vehicleTimeZone);

            List<CarInParkingLotCheAnDTO> carInParkingLotCheAnDTOList = cheAnHttpService.queryCarInParkingLot(vehicleIntegratedConfigBO.getVehicleParkingInfoBO(), QueryCommonCheAnDTO.getInstance(new QueryCarInParkingLotCheAnDTO(AboutDateUtil.dateToString(ingestionDate))));

            biVehicleService.addDwsAccessRecordForHour(centBiConverter.toCreateBiVehicleAccessRecordHourFeignDTO(projectId, parkingLotName, 0, carInParkingLotCheAnDTOList.size(), 0, processDate, ingestionDate));
        } catch (Exception e) {
            log.error("车安车场定时汇总数据到BI发生异常！异常原因：" + e.getMessage());
        }

        log.info("Che An statistics to bi end!");
    }
}
